#include "transfer_network.h"
#include "crossplat_def.h"
CTransferNetwork::CTransferNetwork()
{
	m_pClient = NULL;
}
CTransferNetwork::~CTransferNetwork()
{

}
int CTransferNetwork::Open(const char* dev_name, int parm1, int parm2)
{
	if (m_pClient == NULL)
	{
		m_pClient = new CClientSock();
	}
	int ret = 0;

	ret = m_pClient->Connect(dev_name, parm1, parm2);
	m_port = parm1;
	m_net_type = parm2;

	return 0;
}

void CTransferNetwork::Close()
{
	m_pClient->DisConnect();
}

int CTransferNetwork::ReadData(unsigned char* data, int buf_sz)
{
	int ret =0;
	int wait_cnt = 20;
	int offset = 0;
	CrossSleep(50);
	while (true) 
	{
		ret = m_pClient->RcvData((char*)(data + offset), buf_sz - offset);
		if (ret > 2)
		{
			break;
		}
		if (ret == -1)
		{
			break;
		}

		if (wait_cnt-- <= 0)
		{
			break;
		}
		offset += ret; 
		CrossSleep(100);
	}

	return ret;
}

int  CTransferNetwork::SendData(unsigned char* data, int buf_sz)
{
	int ret = m_pClient->SendRawData((char*)data, buf_sz);
	return ret;
}

void CTransferNetwork::SetDebugMod(int mode)
{
	m_bLog = mode;
}

